'use strict'

const { TaiSpider } = require('tai-spider');
const { formatDate } = require('../utils/str-helper');

const catids = [{
    id: 3486,
    title: 'launch',
}, {
    id: 3551,
    title: 'ground systems',
}, {
    id: 1367,
    title: 'government military'
}];

class SatelliteTodayFullSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'satellite-today-full';
        this.debug = true;
        // this.start_urls = [
        //     'https://www.satellitetoday.com/wp-content/themes/ViaSat/ajax.php',
        //     // 'https://www.satellitetoday.com/category/ground-systems/',
        //     // 'https://www.satellitetoday.com/category/government-military/',
        // ];
    }

    start_urls() {
        let allUrls = [];
        catids.map(cat => {
            for (let i=0; i<10; i++) {
                allUrls.push({
                    link: 'https://www.satellitetoday.com/wp-content/themes/ViaSat/ajax.php',
                    method: 'POST',
                    form: {
                        tracker: i+1,
                        offset: i*10,
                        number: 10,
                        catid: cat.id,
                        action: 'infiscroll_load_category',
                    },
                    catalog: cat.title,
                    skipDuplicates: false,
                });
            }
        });
        return allUrls;
    }

    *parse(response) {
        for (let ele of response.css('article')) {
            yield response.follow(ele.css('.heading a'), this.parseArticle, { catalog: response.options.catalog });
        }
    }

    *parseArticle(response, spider) {
        let data = response.extract();
        let date = formatDate(data.date)
        data.timestamp = date.valueOf();
        data.date = date.format();

        yield Object.assign({
            index_name: 'tai-article',
            catalog: response.options.catalog,
        }, data);
    }
}

module.exports = SatelliteTodayFullSpider;